﻿package cn.poco.pepsi.activity.detail;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import cn.poco.pepsi.R;
import cn.poco.pepsi.TabTest;
import cn.poco.pepsi.bean.FoodPhoto;
import cn.poco.pepsi.bean.Guest;
import cn.poco.pepsi.bean.Restaurant;
import cn.poco.pepsi.service.impl.PhotoImpl;
import cn.poco.pepsi.util.AsyncImageLoader.ImageCallback;
import cn.poco.pepsi.util.AsyncLoadImageService;
import cn.poco.pepsi.util.Cons;
import cn.poco.pepsi.util.Screen;

/**
 * 美食美图图片墙
 * 
 * @author tanglx
 * 
 */
public class FoodPhotoListActivity extends Activity {
	
	private Restaurant res=null;
	private String resid ;
	Bundle bundel = null;

	public static final String TAG = "FoodPhotoListActivity";
	private Context context;
	// 视图
	private FrameLayout linearLayoutHot;
	private GridView gridViewHot;
	private FoodPhotoAdapt fAdapterHot;
	private LinearLayout preLayout;
	private Button foodPhotoResearch;
	// 错误处理
	private LinearLayout error;
	// 无数据处理
	private TextView noDataTextView;

	// 数据
	private Handler tHandler;
	private int type = 1;
	private ArrayList<FoodPhoto> listHot,  listHotRe;//装图片的数组  listNew,, ListNewRe
	ArrayList<Guest> listGuest,listGuestRe;
	private PhotoImpl pImpl;

	// 条件
	private int lBase = 12, lRe = 9;//初始载入12个 
	private int sHot = 0;

	// 标记
	private boolean IsLastDataHot = false;
	private boolean IsLastDataNew = false;
	private boolean isNewFirst = true;

	// 常量
	private static final int show_hot = 1;


	protected void initView() {
		// 区分tab页
	
		linearLayoutHot = (FrameLayout) findViewById(R.id.foodphoto_hot_layout);
		gridViewHot = (GridView) findViewById(R.id.foodphoto_grid_hot);
		preLayout = (LinearLayout) findViewById(R.id.progressbar);
		preLayout.setBackgroundColor(Color.argb(30, 255, 255, 255));
	
	
		// 处理错误信息
		noDataTextView = (TextView) findViewById(R.id.ErrorMessage);

		HandlerThread handlerThread = new HandlerThread(TAG);
		handlerThread.start();
		tHandler = new Handler(handlerThread.getLooper());
	}

	void showTab(int status) {
		switch (status) {
		case show_hot:
			linearLayoutHot.setVisibility(View.VISIBLE);
			break;
	
		default:
			break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.foodphoto_list);
		initView();
		
		//获取餐厅信息
		bundel=getIntent().getExtras();
		resid=bundel.getString(Cons.RES_CODE);
		
		context=this;
		pImpl = new PhotoImpl();

		tHandler.post(new GetImage(type,Integer.parseInt(resid), sHot, lBase, 1));
		sHot = sHot + lBase;



/*foodPhotoResearch.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (type == 1) {
					if (IsLastDataHot == false) {
						tHandler.post(new GetImage(type, Integer.parseInt(resid),sHot, lRe, 2));
						sHot = sHot + lRe;
						foodPhotoResearch.setClickable(false);
						preLayout.setVisibility(View.VISIBLE);
					} 
				}  else {
						Toast.makeText(context, "没有更多数据！", Toast.LENGTH_SHORT).show();
					}
				}
			
		});*/
		
	gridViewHot.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//跳转到餐厅详情
				Intent intent=new Intent(context,PhotoBlogActivity.class);
				intent.putExtra(Cons.RES_CODE, String.valueOf(listHot.get(position).getResId()));
				startActivity(intent);
			}
		});
		
	

	}

	
	
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1://加载最赞
				fAdapterHot = new FoodPhotoAdapt(listHot, context, gridViewHot);
				gridViewHot.setAdapter(fAdapterHot);
				preLayout.setVisibility(View.GONE);
				gridViewHot.setVisibility(View.VISIBLE);
				break;
			case 2://刷新最赞
				preLayout.setVisibility(View.GONE);
				fAdapterHot.notifyDataSetChanged();
				break;
		
			case 5://每次多执行这个
	          // foodPhotoResearch.setClickable(true);
				break;
			case Cons.SHOW_ERROR:
				//出错
				gridViewHot.setVisibility(View.GONE);
				preLayout.setVisibility(View.GONE);
				noDataTextView.setVisibility(View.GONE);
				Toast.makeText(context, msg.obtain().toString(), Toast.LENGTH_SHORT).show();
			default:
				break;
			}
		}
	};

	class GetImage implements Runnable {
		private int tLoadType;
		private int tType;
		private int tResid;
		private int ts;
		private int tl;

		public GetImage(int type,int resid, int s, int l, int loadType) {
			this.tLoadType = loadType;
			this.tType = type;
			this.tResid=resid;
			this.ts = s;
			this.tl = l;
		}

		@Override
		public void run() {
			try {
				switch (tLoadType) {
				//初始载入
				case 1:
					listHot = pImpl.getHotPhoto(tType, tResid,ts, tl);
					if (listHot != null) {
						if (listHot.size() < 12) {
							IsLastDataHot = true;
						}
						System.out.println("listHot:" + listHot.size()+listHot.get(3));
						handler.sendEmptyMessage(1);
					}
					break;
					
					//加载更多
				case 2:
					listHotRe = pImpl.getHotPhoto(tType,tResid, ts, tl);
					if (listHotRe != null) {
						listHot.addAll(0, listHotRe);
						if (listHotRe.size() < 12) {
							IsLastDataHot = true;
						}
						System.out.println("listHotRe:" + listHotRe.size());
						handler.sendEmptyMessage(2);
					}
					break;
				
			
				default:
					break;
				}
				handler.sendEmptyMessage(5);
			} catch (Exception e) {
				e.printStackTrace();
				Message msg = new Message();
				msg.what = Cons.SHOW_ERROR;
				msg.obj = "网络错误";
				handler.sendMessage(msg);
			}
		}

	}
}

/**
 * 美食美图适配器
 * 
 * @author tanglx
 * 
 */
class FoodPhotoAdapt extends BaseAdapter {
	GridView gridView;
	private ArrayList<FoodPhoto> vector;
	private Context context;
	private AsyncLoadImageService aLoader;
	int withAndHight = 119;

	FoodPhotoAdapt(ArrayList<FoodPhoto> vector, Context context, GridView gridView) {
		this.vector = vector;
		this.context = context;
		this.gridView = gridView;
		this.aLoader = new AsyncLoadImageService(context);
	}

	@Override
	public int getCount() {
		return vector.size();
	}

	@Override
	public Object getItem(int position) {

		return vector.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.food_photo_list_item, null);
			viewHolder.imageView = (ImageView) convertView.findViewById(R.id.srcFirstImage);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		final String imgUrl = vector.get(position).getImgUrl();
		if (imgUrl != null && !imgUrl.equals("")) {
			viewHolder.imageView.setTag(imgUrl);
			Bitmap drawUserIcon = aLoader.loadBitmap(imgUrl, new ImageCallback() {
				@Override
				public void imageLoaded(Bitmap imageBitmap, String imageUrl) {
					ImageView imageViewByTag = (ImageView) gridView.findViewWithTag(imgUrl);
					if (imageViewByTag != null) {
						imageViewByTag.setImageBitmap(imageBitmap);
					}
				}
			}, false, false, Screen.dip2px(context, 65));
			if (drawUserIcon == null) {
				viewHolder.imageView.setImageResource(R.drawable.pps);
			} else {
				viewHolder.imageView.setImageBitmap(drawUserIcon);
			}
		} else {
			viewHolder.imageView.setImageResource(R.drawable.pps);
		}
		return convertView;
	}

	class ViewHolder {
		ImageView imageView;
		TextView textView;
	}
}
